package org.chen.wordwave.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.toast.Toaster;

import org.chen.wordwave.R;
import org.chen.wordwave.act.Cn_EnChoice;
import org.chen.wordwave.act.FavoriteWordsAct;
import org.chen.wordwave.act.Practice;
import org.chen.wordwave.act.Review;
import org.chen.wordwave.act.Statistic;
import org.chen.wordwave.act.Translator;
import org.chen.wordwave.act.game.GameSelection;
import org.chen.wordwave.bean.FavoriteWords;
import org.chen.wordwave.bean.UserInfo;
import org.chen.wordwave.bean.Words;
import org.chen.wordwave.bean.WordsRvItem;
import org.chen.wordwave.init.WordInit;
import org.chen.wordwave.tools.user.UserUtils;
import org.chen.wordwave.tools.words.WordsUtils;
import org.litepal.LitePal;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class WordsFragment extends Fragment {

    private RecyclerView rv_words;
    private List<WordsRvItem> wordsRvItems;
    private Context mContext;
    private UserInfo userInfo;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= View.inflate(getActivity(), R.layout.fragment_words,null);
        mContext=getActivity();
        rv_words = view.findViewById(R.id.rv_words);
        initData();
        rv_words.setLayoutManager(new GridLayoutManager(getContext(),2));
        rv_words.setAdapter(new WordsItemAdapter());
        return view;
    }

    private void initData() {
        wordsRvItems=new ArrayList<>();
        wordsRvItems.add(new WordsRvItem(R.drawable.ywxy_green,getString(R.string.english_choice)));
        wordsRvItems.add(new WordsRvItem(R.drawable.zwxc_green,getString(R.string.chinese_word_selection)));
        wordsRvItems.add(new WordsRvItem(R.drawable.pxtk_green,getString(R.string.new_vocabulary_book)));
        wordsRvItems.add(new WordsRvItem(R.drawable.pxzh_green,getString(R.string.learning_tests)));
        wordsRvItems.add(new WordsRvItem(R.drawable.qplx_green,getString(R.string.complete_spelling_exercise)));
        wordsRvItems.add(new WordsRvItem(R.drawable.review2,getString(R.string.listening_to_sounds_and_distinguishing_meanings)));
        wordsRvItems.add(new WordsRvItem(R.drawable.total,getString(R.string.listening_to_sonic_records)));
        wordsRvItems.add(new WordsRvItem(R.drawable.game,getString(R.string.game)));
        userInfo = UserUtils.getCurrentUser(getContext());
    }


    class WordsItemAdapter extends RecyclerView.Adapter<WordsItemAdapter.WordsItemHolder>{


        @NonNull
        @Override
        public WordsItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(mContext).inflate(R.layout.item_words_item, parent, false);
            return new WordsItemHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull WordsItemHolder holder, int position) {
            WordsRvItem wordsRvItem=wordsRvItems.get(position);
            holder.iv_image.setImageResource(wordsRvItem.getResId());
            holder.tv_desc.setText(wordsRvItem.getDesc());
            holder.rl_words.setOnClickListener((e)->{
                switch (position){
                    case 0:
                        if (WordInit.userInfo.getEmail() != null){
                            Intent intent =new Intent(mContext, Cn_EnChoice.class);
                            intent.putExtra("words", (Serializable) WordsUtils.getFlyerWords(null));
                            intent.putExtra("type",1);
                            startActivity(intent);
                        }else {
                            Toaster.show(getString(R.string.need_login));
                        }
                        break;
                    case 1:
                        if (WordInit.userInfo.getEmail() != null){
                            Intent intent1 =new Intent(mContext,Cn_EnChoice.class);
                            intent1.putExtra("words", (Serializable) WordsUtils.getFlWords());
                            intent1.putExtra("type",2);
                            startActivity(intent1);
                        }else {
                            Toaster.show(getString(R.string.need_login));
                        }
                        break;
                    case 2:
                        if (WordInit.userInfo.getEmail() != null){
                            List<FavoriteWords> favoriteWords= LitePal.where("email = ?",WordInit.userInfo.getEmail()).find(FavoriteWords.class);
                            List<Words> wordsList =new ArrayList<>();
                            for (FavoriteWords favoriteWords1:favoriteWords){
                                wordsList.add(LitePal.where("id = ?",String.valueOf(favoriteWords1.getWordsId())).findFirst(Words.class));
                            }
                            Intent intent2 =new Intent(mContext, FavoriteWordsAct.class);
                            intent2.putExtra("favWords", (Serializable) wordsList);
                            startActivity(intent2);
                        }else {
                            Toaster.show(getString(R.string.need_login));
                        }
                        break;
                    case 3:
                        if (userInfo.isMember() && userInfo.getMembershipExpiryDate() != null){
                            startActivity(new Intent(mContext, Practice.class));
                        }else {
                            Toaster.show(getString(R.string.please_become_a_member_first));
                        }
                        break;
                    case 4:
                        startActivity(new Intent(mContext, Translator.class));
                        break;
                    case 5:
                        startActivity(new Intent(mContext, Review.class));
                        break;
                    case 6:
                        startActivity(new Intent(mContext, Statistic.class));
                        break;
                    case 7:
                        if (userInfo.isMember() && userInfo.getMembershipExpiryDate() != null){
                            startActivity(new Intent(mContext, GameSelection.class));
                        }else {
                            Toaster.show(getString(R.string.please_become_a_member_first));
                        }
                        break;
                    default:
                        break;
                }
            });

        }
        @Override
        public int getItemCount() {
            return wordsRvItems.size();
        }

        class WordsItemHolder extends RecyclerView.ViewHolder {
            private ImageView iv_image;
            private TextView tv_desc;
            private RelativeLayout rl_words;

            public WordsItemHolder(View itemView) {
                super(itemView);
                iv_image = itemView.findViewById(R.id.iv_image);
                tv_desc = itemView.findViewById(R.id.tv_desc);
                rl_words = itemView.findViewById(R.id.rl_words);
            }
        }
    }

    private UserInfo getCurrentUser() {
        return UserUtils.getCurrentUser(getContext());
    }

    @Override
    public void onResume() {
        super.onResume();
        userInfo = getCurrentUser();
    }
}
